/*
 * > /*
>  * INSANE - Interactive Structural Analysis Environment
>  *
>  * Copyright (C) 2003-2004
>  * Universidade Federal de Minas Gerais
>  * Escola de Engenharia
>  * Departamento de Engenharia de Estruturas
>  *
>  * Author's email :     insane@dees.ufmg.br
>  * Author's Website :   http://www.dees.ufmg.br/insane
>  *
>  * This program is free software; you can redistribute it and/or
>  * modify it under the terms of the GNU General Public License
>  * as published by the Free Software Foundation; either version 2
>  * of the License, or any later version.
>  *
>  * This program is distributed in the hope that it will be useful,
>  * but WITHOUT ANY WARRANTY; without even the implied warranty of
>  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
>  * GNU General Public License for more details.
>  *
>  * You should have received a copy of the GNU General Public License
>  * along with this program; if not, write to the Free Software
>  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-
> 1307, USA.
 *
 */

package br.ufmg.dees.insane.xyplot.xyplot_view.dialog;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.event.ContainerAdapter;
import java.awt.event.ContainerEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JComboBox;
import javax.swing.JDialog;

/**
 * A dialog box that handles window closing, the ENTER key and the ESCAPE key for you. All you have to do is implement ok() (called when Enter is pressed) and cancel() (called when Escape is pressed, or window is closed). Adapted from JEdit source code (http://www.jedit.org)
 * @author  Slava Pestov
 */
public abstract class EnhancedDialog extends JDialog {
	
	
	protected KeyHandler keyHandler;
	
	
//*****************************************************************************
	
	public EnhancedDialog(Frame parent, String title, boolean modal) {
		super(parent,title,modal);
		_init();
	}
	
//*****************************************************************************
	
	public EnhancedDialog(Dialog parent, String title, boolean modal) {
		super(parent,title,modal);
		_init();
	}
	
//*****************************************************************************
	
	public abstract void ok();
	public abstract void cancel();
	
//*****************************************************************************
	
	private void _init() {
		((Container)getLayeredPane()).addContainerListener(
			new ContainerHandler());
		getContentPane().addContainerListener(new ContainerHandler());
		
		keyHandler = new KeyHandler();
		addKeyListener(keyHandler);
		addWindowListener(new WindowHandler());
		
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
	}
	
//*****************************************************************************
	
	// Recursively adds our key listener to sub-components
	class ContainerHandler extends ContainerAdapter {
		public void componentAdded(ContainerEvent evt) {
			componentAdded(evt.getChild());
		}
		
	//*****************************************************************************
		
		public void componentRemoved(ContainerEvent evt) {
			componentRemoved(evt.getChild());
		}
		
	//*****************************************************************************
		
		private void componentAdded(Component comp) {
			comp.addKeyListener(keyHandler);
			if(comp instanceof Container) {
				Container cont = (Container)comp;
				cont.addContainerListener(this);
				Component[] comps = cont.getComponents();
				for(int i = 0; i < comps.length; i++) {
					componentAdded(comps[i]);
				}
			}
		}
		
	//*****************************************************************************
		
		private void componentRemoved(Component comp) {
			comp.removeKeyListener(keyHandler);
			if(comp instanceof Container) {
				Container cont = (Container)comp;
				cont.removeContainerListener(this);
				Component[] comps = cont.getComponents();
				for(int i = 0; i < comps.length; i++) {
					componentRemoved(comps[i]);
				}
			}
		}
	//*****************************************************************************
	}
	
//*****************************************************************************
	
	class KeyHandler extends KeyAdapter {
		public void keyPressed(KeyEvent evt) {
			if(evt.isConsumed())
				return;
			
			if(evt.getKeyCode() == KeyEvent.VK_ENTER) {
				// crusty workaround
				Component comp = getFocusOwner();
				while(comp != null) {
					if(comp instanceof JComboBox) {
						JComboBox combo = (JComboBox)comp;
						if(combo.isEditable()) {
							Object selected = combo.getEditor().getItem();
							if(selected != null)
								combo.setSelectedItem(selected);
						}
						break;
					}
					
					comp = comp.getParent();
				}
				
				ok();
				evt.consume();
			}
			else if(evt.getKeyCode() == KeyEvent.VK_ESCAPE)	{
				cancel();
				evt.consume();
			}
		}
	}
	
//*****************************************************************************
	
	class WindowHandler extends WindowAdapter {
		
		public void windowClosing(WindowEvent evt) {
			cancel();
		}
	}
	
//*****************************************************************************
}
